//
//  WCAppDelegate.m
//  WeboCon
//
//  Created by black sun on 12-2-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WCAppDelegate.h"

@implementation WCAppDelegate
@synthesize loginController;
@synthesize window = _window;
@synthesize splitView;
@synthesize leftViewController;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    
    self.splitView.delegate = self;
    
    WCLeftViewController *leftController = [[WCLeftViewController alloc] initWithNibName:@"WCLeftViewController" bundle:nil];
    self.leftViewController = leftController;
    [self.splitView addSubview:leftViewController.view];
    //[self.window w]
    NSView *view = [[NSView alloc] initWithFrame:CGRectMake(60, 0, 400, 400)];
    [self.splitView addSubview:view];
    
    if (![[WCUserContext sharedInstance] hasLogin]) {
        WCSinaLoginWindowController *loginWindowController = [[WCSinaLoginWindowController alloc] initWithWindowNibName:@"WCSinaLoginWindowController"];
        self.loginController = loginWindowController;
        //[NSApp beginSheetModalForWindow:loginWindowController.window modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
        [self.window addChildWindow:loginWindowController.window ordered:NSWindowAbove];
    }
}



// Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
{
    return [[[WCCoreDataContext sharedInstance] managedObjectContext] undoManager];
}

// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
- (IBAction)saveAction:(id)sender
{
    
    NSError *error = nil;
    if (![[WCCoreDataContext sharedInstance].managedObjectModel commitEditing]) {
        NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
    }
    
    if (![[WCCoreDataContext sharedInstance].managedObjectContext save:&error]) {
        [[NSApplication sharedApplication] presentError:error];
    }
}
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
    [sheet orderOut:self];
    
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    return NSTerminateNow;
    // Save changes in the application's managed object context before the application terminates.
    NSManagedObjectContext *__managedObjectContext = [WCCoreDataContext sharedInstance].managedObjectContext;
    
    if (!__managedObjectContext) {
        return NSTerminateNow;
    }
    
    if (![__managedObjectContext commitEditing]) {
        NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
        return NSTerminateCancel;
    }
    
    if (![__managedObjectContext hasChanges]) {
        return NSTerminateNow;
    }
    
    NSError *error = nil;
    if (![__managedObjectContext save:&error]) {

        // Customize this code block to include application-specific recovery steps.              
        BOOL result = [sender presentError:error];
        if (result) {
            return NSTerminateCancel;
        }

        NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
        NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
        NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
        NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:question];
        [alert setInformativeText:info];
        [alert addButtonWithTitle:quitButton];
        [alert addButtonWithTitle:cancelButton];

        NSInteger answer = [alert runModal];
        
        if (answer == NSAlertAlternateReturn) {
            return NSTerminateCancel;
        }
    }

    return NSTerminateNow;
}

#pragma mark NSSplitViewDelegate
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex {
    return 60;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex {
    return 60;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview{
    return NO;
}

@end
